package com.example.maybeaccount.Entry;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.example.maybeaccount.Mode.Account;
import com.example.maybeaccount.Mode.SqliteDB;
import com.example.maybeaccount.R;

/**
 * 图形密码注册
 */
public class NineGridRegister extends AppCompatActivity implements LockPatternView.OnPatternChangeListener {

    private LockPatternView lpv;
    private TextView tv;
    private User user;
    private int inputTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nine_grid_register);

        lpv = (LockPatternView) findViewById(R.id.lock_pattern_view);
        tv = (TextView) findViewById(R.id.text);
        lpv.setOnPatternChangeListener(this);
        Intent intent = this.getIntent();
        user = (User)intent.getSerializableExtra("user");
        int i = 0;
    }

    @Override
    public void onPatternChange(String patternPassword) {
        if (patternPassword == null) {
            tv.setText("请至少5个点");
        } else if(inputTime == 0) {
            tv.setText("请再画一次");
            user.setGridPwd(patternPassword);
            lpv.clearDraw();
            inputTime ++;
        } else if(inputTime == 1 && !user.getGridPwd().equals(patternPassword)) {
            tv.setText("不一致，再画一次");
            lpv.clearDraw();
            inputTime  = 0;
        } else if(inputTime == 1) {
            tv.setText("设置成功");
            user.setGridPwd(patternPassword);

            //add for debug
            //Toast.makeText(getApplicationContext(),"debug:"+user.getGridPwd(),Toast.LENGTH_LONG).show();

            int result= SqliteDB.getInstance(getApplicationContext()).saveUser(user);
            if (result==1){
                //自动生成默认账户
                Account alipay = new Account("支付宝","移动支付",0.0,"无");
                Account wechat = new Account("微信支付","移动支付",0.0,"无");
                Account pingan = new Account("平安银行卡","银行卡",0.0,"无");
                result = SqliteDB.getInstance(getApplicationContext()).saveAccount(alipay);
                result *= SqliteDB.getInstance(getApplicationContext()).saveAccount(wechat);
                result *= SqliteDB.getInstance(getApplicationContext()).saveAccount(pingan);

                //跳转
                if(result == 1){
                    Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(NineGridRegister.this, Entrance.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(getApplicationContext(),"抱歉，初始化失败，请重新安装T_T",Toast.LENGTH_LONG).show();
                }

            }else if (result==-1) {
                Toast.makeText(getApplicationContext(),"用户名已存在",Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(getApplicationContext(),"出错！！！",Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    public void onPatternStarted(boolean isStarted) {
        if (isStarted) {
            tv.setText("请绘制图案");
        }

    }
}
